package com.jb.web.service;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.SearchParameters;

import com.jb.web.bean.ListBean;
import com.jb.web.bean.event.UIEvent;
import com.jb.web.context.WebContext;

import java.util.List;


public class FlickrEventHandler {
    List search(String key, String[] query) {
        try {
            Flickr flickr = new Flickr(key);
            SearchParameters sp = new SearchParameters();
            sp.setTags(query);
            List list = flickr.getPhotosInterface().search(sp, 24, 1);
            for (int i = 0; i < list.size(); i++) {
                Photo photo = (Photo)list.get(i);
                photo.setUrl("http://static.flickr.com/" 
                 + photo.getServer() +"/" + photo.getId() 
                 + "_" + photo.getSecret());
            }
            return list;
        } catch (Exception e) {
            return null;
        }
    }
    public void onSearch(WebContext ctx, UIEvent e) {
        List list = search(e.get("key"), e.get("query").split(" "));
        ListBean bean = (ListBean)ctx.getRequestComponent("photos");
        bean.setList(list);
    }
}
